﻿using System;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using Entities;
using Business;
using System.Web.UI.WebControls;
namespace ServicesDirectory.UserControl
{
    public partial class ServiceDetail : System.Web.UI.UserControl
    {
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            lbtDetail1.Click += LbtDetail1Click;
            lbtDetail2.Click += LbtDetail2Click;
            lbtDetail3.Click += LbtDetail3Click;
            lbtDetail4.Click += LbtDetail4Click;
            lbtDetail5.Click += LbtDetail5Click;
            lbtContract.Click += LbtContractClick;
            lbtFunding.Click += LbtFundingClick;
            cmdBack.Click += CmdBack_Click;
            btnInactive.Click += BtnInactive_Click;
            cmdSave.Click += CmdSaveClick;
            ServiceDetail11.AppearedTab4 += ServiceDetail11AppearedTab4;
            var serviceId = Request.QueryString["ServiceId"];
            if (!IsPostBack)
            {
                ServiceContract1.LoadReferenceData();
                ServiceDetail11.LoadReferenceData();
                ServiceDetail2.LoadRefrenceData();
                ServiceDetail3.LoadReferenceData();
                ServiceFunding1.LoadReferenceData();
                if (serviceId == null)
                {
                    ViewState["ModeForm"] = "Add";

                    lbtDetail4.Visible = false;
                    lbtDetail5.Visible = false;
                    pnDetail1.Visible = true;
                    pnDetail2.Visible = false;
                    pnDetail3.Visible = false;
                    pnDetail4.Visible = false;
                    pnDetail5.Visible = false;
                    pnContract.Visible = false;
                    pnFunding.Visible = false;
                    btnInactive.Visible = false;

                }
                else
                {
                    ViewState["serviceId"] = serviceId;
                    Session["serviceId"] = serviceId;
                    if (Session["Copy"] == null)
                    {
                        ViewState["ModeForm"] = "Edit";
                    }
                    else
                    {
                        ViewState["ModeForm"] = "Copy";
                        Session.Remove("Copy");
                    }
                    BindData(serviceId);
                }
                if (Request.QueryString["active"] != null)
                {
                    var serviceIdactive = new Guid(Request.QueryString["active"]);
                    var result = ServiceBusiness.Instance.ChangeStatus(serviceIdactive, true);
                    if (result)
                    {
                        Session["serviceId"] = serviceIdactive.ToString();
                        Response.Redirect("ServiceDetail.aspx?ServiceId=" + serviceIdactive);
                    }
                }

            }
        }

        /// <summary>
        /// Services the detail11 appeared tab4.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        private void ServiceDetail11AppearedTab4(object sender, EventArgs e)
        {
            var mode = sender.ToString();
            switch (mode)
            {
                case "2":
                    lbtContract.Visible = true;
                    lbtFunding.Visible = false;
                    lbtDetail1.CssClass = "";
                    lbtDetail2.CssClass = "";
                    lbtDetail3.CssClass = "";
                    lbtDetail4.CssClass = "";
                    lbtDetail5.CssClass = "";
                    lbtContract.CssClass = "active";
                    lbtFunding.CssClass = "";

                    pnDetail1.Visible = false;
                    pnDetail2.Visible = false;
                    pnDetail3.Visible = false;
                    pnDetail4.Visible = false;
                    pnDetail5.Visible = false;
                    pnContract.Visible = true;
                    pnFunding.Visible = false;
                    break;
                case "3":
                    lbtFunding.Visible = true;
                    lbtContract.Visible = false;
                    lbtDetail1.CssClass = "";
                    lbtDetail2.CssClass = "";
                    lbtDetail3.CssClass = "";
                    lbtDetail4.CssClass = "";
                    lbtDetail5.CssClass = "";
                    lbtContract.CssClass = "";
                    lbtFunding.CssClass = "active";

                    pnDetail1.Visible = false;
                    pnDetail2.Visible = false;
                    pnDetail3.Visible = false;
                    pnDetail4.Visible = false;
                    pnDetail5.Visible = false;
                    pnContract.Visible = false;
                    pnFunding.Visible = true;
                    break;
                default:
                    lbtFunding.Visible = false;
                    lbtContract.Visible = false;
                    break;
            }
        }

        /// <summary>
        /// LBTs the funding click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        private void LbtFundingClick(object sender, EventArgs e)
        {
            lbtDetail1.CssClass = "";
            lbtDetail2.CssClass = "";
            lbtDetail3.CssClass = "";
            lbtDetail4.CssClass = "";
            lbtDetail5.CssClass = "";
            lbtContract.CssClass = "";
            lbtFunding.CssClass = "active";

            pnDetail1.Visible = false;
            pnDetail2.Visible = false;
            pnDetail3.Visible = false;
            pnDetail4.Visible = false;
            pnDetail5.Visible = false;
            pnContract.Visible = false;
            pnFunding.Visible = true;
        }

        /// <summary>
        /// LBTs the contract click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        private void LbtContractClick(object sender, EventArgs e)
        {
            lbtDetail1.CssClass = "";
            lbtDetail2.CssClass = "";
            lbtDetail3.CssClass = "";
            lbtDetail4.CssClass = "";
            lbtDetail5.CssClass = "";
            lbtContract.CssClass = "active";
            lbtFunding.CssClass = "";

            pnDetail1.Visible = false;
            pnDetail2.Visible = false;
            pnDetail3.Visible = false;
            pnDetail4.Visible = false;
            pnDetail5.Visible = false;
            pnContract.Visible = true;
            pnFunding.Visible = false;
        }

        /// <summary>
        /// Handles the Click event of the BtnInactive control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        private void BtnInactive_Click(object sender, EventArgs e)
        {
            var script = "var result= window.confirm('Do you want to make this Service in-active?'); " +
                         "if(result)" +
                         " location.href=('ListService.aspx?inactive=" + Session["serviceId"].ToString() + "');";

            ScriptManager.RegisterClientScriptBlock(UpdatePanel1, typeof(UpdatePanel), "ServiceDetailKey", script, true);
        }

        /// <summary>
        /// Handles the Click event of the CmdBack control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        private void CmdBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("ListService.aspx",true);
        }

        /// <summary>
        /// LBTs the detail5 click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        private void LbtDetail5Click(object sender, EventArgs e)
        {
            lbtDetail1.CssClass = "";
            lbtDetail2.CssClass = "";
            lbtDetail3.CssClass = "";
            lbtDetail4.CssClass = "";
            lbtDetail5.CssClass = "active";
            lbtContract.CssClass = "";
            lbtFunding.CssClass = "";

            pnDetail1.Visible = false;
            pnDetail2.Visible = false;
            pnDetail3.Visible = false;
            pnDetail4.Visible = false;
            pnDetail5.Visible = true;
            pnContract.Visible = false;
            pnFunding.Visible = false;
        }

        /// <summary>
        /// LBTs the detail4 click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        private void LbtDetail4Click(object sender, EventArgs e)
        {
            lbtDetail1.CssClass = "";
            lbtDetail2.CssClass = "";
            lbtDetail3.CssClass = "";
            lbtDetail4.CssClass = "active";
            lbtDetail5.CssClass = "";
            lbtContract.CssClass = "";
            lbtFunding.CssClass = "";


            pnDetail1.Visible = false;
            pnDetail2.Visible = false;
            pnDetail3.Visible = false;
            pnDetail4.Visible = true;
            pnDetail5.Visible = false;

            pnContract.Visible = false;
            pnFunding.Visible = false;
        }

        /// <summary>
        /// LBTs the detail3 click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        private void LbtDetail3Click(object sender, EventArgs e)
        {

            lbtDetail1.CssClass = "";
            lbtDetail2.CssClass = "";
            lbtDetail3.CssClass = "active";
            lbtDetail4.CssClass = "";
            lbtDetail5.CssClass = "";
            lbtContract.CssClass = "";
            lbtContract.CssClass = "";

            pnDetail1.Visible = false;
            pnDetail2.Visible = false;
            pnDetail3.Visible = true;
            pnDetail4.Visible = false;
            pnDetail5.Visible = false;
            pnContract.Visible = false;
            pnFunding.Visible = false;
        }

        /// <summary>
        /// LBTs the detail2 click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        private void LbtDetail2Click(object sender, EventArgs e)
        {

            lbtDetail1.CssClass = "";
            lbtDetail2.CssClass = "active";
            lbtDetail3.CssClass = "";
            lbtDetail4.CssClass = "";
            lbtDetail5.CssClass = "";
            lbtContract.CssClass = "";
            lbtFunding.CssClass = "";

            pnDetail1.Visible = false;
            pnDetail2.Visible = true;
            pnDetail3.Visible = false;
            pnDetail4.Visible = false;
            pnDetail5.Visible = false;
            pnContract.Visible = false;
            pnFunding.Visible = false;
        }

        /// <summary>
        /// LBTs the detail1 click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        private void LbtDetail1Click(object sender, EventArgs e)
        {

            lbtDetail1.CssClass = "active";
            lbtDetail2.CssClass = "";
            lbtDetail3.CssClass = "";
            lbtDetail4.CssClass = "";
            lbtDetail5.CssClass = "";
            lbtFunding.CssClass = "";
            lbtContract.CssClass = "";

            pnDetail1.Visible = true;
            pnDetail2.Visible = false;
            pnDetail3.Visible = false;
            pnDetail4.Visible = false;
            pnDetail5.Visible = false;
            pnContract.Visible = false;
            pnFunding.Visible = false;
        }

        /// <summary>
        /// CMDs the save click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        private void CmdSaveClick(object sender, EventArgs e)
        {
            var service = CreateNewService();
            string script;
            string message;
            if (string.IsNullOrEmpty(ViewState["ModeForm"].ToString()) || ViewState["ModeForm"].ToString().Equals("Add"))
            {
                
                if (ServiceBusiness.Instance.InsertService(service,out message))
                {
                    script = "var result=window.confirm('Add new Service success! Are you want to add another?'); " +
                              "if(!result)" +
                              " location.href=('ListService.aspx');";
                    ScriptManager.RegisterClientScriptBlock(UpdatePanel1,typeof(UpdatePanel),"addsuccess",script,true);
                }

                else
                {
                    script = @"alert('"+message+" Please check your input and try again!');";
                    ScriptManager.RegisterClientScriptBlock(UpdatePanel1,typeof(UpdatePanel),"addfail",script,true);
                }
            }
            else
            {
                //edit
                if (ViewState["ModeForm"].ToString().Equals("Edit"))
                {
                   
                        
                    
                    if (LogDataBusiness.Instance.GetSessionByIdRecord(service.ServiceId.ToString()) == Session.SessionID)
                    {
                        var result = ServiceBusiness.Instance.UpdateService(service,out message);
                        if (result)
                        {
                            script = "var result=window.confirm('Update service success! Are you want to continue amend?'); " +
                                      "if(!result)" +
                                      " location.href=('ListService.aspx?log=log');";
                            ScriptManager.RegisterClientScriptBlock(UpdatePanel1, typeof(UpdatePanel), "updatesuccess", script, true);
                        }
                        else
                        {
                            script = @"alert('" + message + " Please check your input and try again!');";
                            ScriptManager.RegisterClientScriptBlock(UpdatePanel1, typeof(UpdatePanel), "updatefail", script, true);
                        }
                    }

                    else
                    {
                        script = "alert('Record is in use. Please wait!');";
                        ScriptManager.RegisterClientScriptBlock(UpdatePanel1, typeof(UpdatePanel), "inuse", script, true);
                    }
                }
                //copy
                else
                {
                    if (ServiceBusiness.Instance.InsertService(service,out message))
                    {
                        lblMessage.Text = "Copy Successfull!";
                        lblMessage.Visible = true;
                    }

                    else
                    {
                        lblMessage.Text = "Copy fail";
                        lblMessage.Visible = true;
                    }
                }
            }
        }

        /// <summary>
        /// Binds the data.
        /// </summary>
        /// <param name="serviceId">The service id.</param>
        private void BindData(string serviceId)
        {
            var service = ServiceBusiness.Instance.GetById(new Guid(serviceId));
            if (service != null)
            {

                var txtServiceName = ServiceDetail11.FindControl("txtServiceName") as TextBox;
                if (txtServiceName != null)
                {
                    if (ViewState["ModeForm"].Equals("Copy"))
                    {
                        txtServiceName.Text = "";
                    }
                    else
                    {
                        txtServiceName.Text = HttpUtility.HtmlDecode(service.ServiceName);
                        //write log
                        var logData = new LogData
                                          {
                                              LogDate = DateTime.Now,
                                              LogId = Guid.NewGuid(),
                                              IdRecord = service.ServiceId,
                                              SessionId = Session.SessionID
                                          };
                        LogDataBusiness.Instance.Insert(logData);
                    }
                }
                var chkActive = ServiceDetail11.FindControl("chkActive") as CheckBox;
                if (chkActive != null)
                {
                    chkActive.Checked = service.ServiceActive;
                }

                var txtShortDes = ServiceDetail11.FindControl("txtShortDes") as TextBox;
                if (txtShortDes != null)
                {

                    txtShortDes.Text = HttpUtility.HtmlDecode(service.ShortDescription);

                }
                var txtFullDes = ServiceDetail11.FindControl("txtFullDes") as TextBox;
                if (txtFullDes != null)
                {
                    txtFullDes.Text = HttpUtility.HtmlDecode(service.FullDescription);
                }
                var ddlSubType = ServiceDetail11.FindControl("ddlSubType") as DropDownList;
                if (ddlSubType != null)
                {
                    ddlSubType.SelectedValue = service.SubType.ToString(CultureInfo.InvariantCulture);
                    switch (service.SubType.ToString(CultureInfo.InvariantCulture))
                    {
                        case "2":
                            lbtContract.Visible = true;
                            lbtFunding.Visible = false;
                            break;
                        case "3":
                            lbtFunding.Visible = true;
                            lbtContract.Visible = false;

                            break;
                        default:
                            lbtFunding.Visible = false;
                            lbtContract.Visible = false;
                            break;
                    }

                }
                var txtDeptCode = ServiceDetail11.FindControl("txtDeptCode") as TextBox;
                if (txtDeptCode != null)
                {
                    txtDeptCode.Text = HttpUtility.HtmlDecode(service.DeptCode);
                }

                var lbtLookup = ServiceDetail11.FindControl("lbtLookup") as LinkButton;
                var lblLeadContact = ServiceDetail11.FindControl("lblLeadContact") as TextBox;
                if (lbtLookup != null && lblLeadContact != null)
                {
                    string announce;
                    var contact = ContactBusiness.Instance.GetContact(service.ContactId.ToString(), out announce);
                    if (contact != null)
                    {
                        lblLeadContact.Enabled = true;                       
                        lblLeadContact.Text = contact.FirstName + " " + contact.SurName;
                        lbtLookup.CommandArgument = contact.ContactId.ToString();
                        lblLeadContact.Enabled = false;  
                    }
                }
                var ddlServiceType = ServiceDetail11.FindControl("ddlServiceType") as DropDownList;
                if (ddlServiceType != null)
                {
                    ddlServiceType.SelectedValue = service.ServiceType.ToString(CultureInfo.InvariantCulture);
                }
                var txtClientDes = ServiceDetail11.FindControl("txtClientDes") as TextBox;
                if (txtClientDes != null)
                {

                    txtClientDes.Text = HttpUtility.HtmlDecode(service.ClientDescription);

                }
                var txtDesDelivery = ServiceDetail11.FindControl("txtDesDelivery") as TextBox;
                if (txtDesDelivery != null)
                {

                    txtDesDelivery.Text = HttpUtility.HtmlDecode(service.DescriptionDelivery);

                }
                var ddlAttendace = ServiceDetail11.FindControl("ddlAttendace") as DropDownList;
                if (ddlAttendace != null)
                {

                    ddlAttendace.SelectedValue = service.ServiceAttendance;

                }
                var txtContactCode = ServiceDetail11.FindControl("txtContactCode") as TextBox;
                if (txtContactCode != null)
                {

                    txtContactCode.Text = HttpUtility.HtmlDecode(service.ContractCode);

                }
                var txtStartExpected = ServiceDetail11.FindControl("txtStartExpected") as TextBox;
                if (txtStartExpected != null)
                {
                    DateTime fundingStart;
                    txtStartExpected.Text = DateTime.TryParse(service.StartExpected, out fundingStart)
                                                ? fundingStart.ToString("dd/MM/yyyy",CultureInfo.InvariantCulture)
                                                : string.Empty;
                }
                var txtStartDate = ServiceDetail11.FindControl("txtStartDate") as TextBox;
                if (txtStartDate != null)
                {
                    DateTime fundingStart;
                    txtStartDate.Text = DateTime.TryParse(service.StartDate, out fundingStart)
                                            ? fundingStart.ToString("dd/MM/yyyy",CultureInfo.InvariantCulture)
                                            : string.Empty;
                }
                var txtEndDate = ServiceDetail11.FindControl("txtEndDate") as TextBox;
                if (txtStartExpected != null)
                {
                    DateTime fundingStart;
                    txtEndDate.Text = DateTime.TryParse(service.EndDate, out fundingStart)
                                          ? fundingStart.ToString("dd/MM/yyyy",CultureInfo.InvariantCulture)
                                          : string.Empty;
                }
                var txtContractValue = ServiceDetail11.FindControl("txtContractValue") as TextBox;
                if (txtContractValue != null)
                {
                    txtContractValue.Text = HttpUtility.HtmlDecode(service.ContractValue);
                }
                var chkPayment = ServiceDetail11.FindControl("chkPayment") as CheckBox;
                if (chkPayment != null)
                {
                    chkPayment.Checked = service.StagedPayment;
                }
                var ddlProcessMethod = ServiceDetail11.FindControl("ddlProcessMethod") as DropDownList;
                if (ddlProcessMethod != null)
                {

                    ddlProcessMethod.SelectedValue = service.ProcessMethod;

                }
                var chkExtendable = ServiceDetail11.FindControl("chkExtendable") as CheckBox;
                if (chkExtendable != null)
                {
                    if (!string.IsNullOrEmpty(service.Extendable))
                    {
                        chkExtendable.Checked = true;

                        var ddlYearExtendable = ServiceDetail11.FindControl("ddlYearExtendable") as DropDownList;
                        var ddlMonthExtendable = ServiceDetail11.FindControl("ddlMonthExtendable") as DropDownList;

                        if (ddlYearExtendable != null && ddlMonthExtendable != null)
                        {
                            ddlYearExtendable.Enabled = true;
                            ddlMonthExtendable.Enabled = true;
                            ddlMonthExtendable.SelectedValue = service.Extendable.Split('#')[0];
                            ddlYearExtendable.SelectedValue = service.Extendable.Split('#')[1];

                        }
                    }
                    else
                    {
                        chkExtendable.Checked = false;

                        var ddlYearExtendable = ServiceDetail11.FindControl("ddlYearExtendable") as DropDownList;
                        var ddlMonthExtendable = ServiceDetail11.FindControl("ddlMonthExtendable") as DropDownList;

                        if (ddlYearExtendable != null && ddlMonthExtendable != null)
                        {
                            ddlYearExtendable.Enabled = false;
                            ddlMonthExtendable.Enabled = false;
                        }
                    }
                }
                var chkTimelimited = ServiceDetail11.FindControl("chkTimelimited") as CheckBox;
                if (chkTimelimited != null)
                {
                    if (!string.IsNullOrEmpty(service.TimeLimited))
                    {
                        chkTimelimited.Checked = true;
                        var ddlYearLimit = ServiceDetail11.FindControl("ddlYearLimit") as DropDownList;
                        var ddlMonthLimit = ServiceDetail11.FindControl("ddlMonthLimit") as DropDownList;

                        if (ddlYearLimit != null && ddlMonthLimit != null)
                        {
                            ddlMonthLimit.Enabled = true;
                            ddlYearLimit.Enabled = true;
                            ddlMonthLimit.SelectedValue = service.TimeLimited.Split('#')[0];
                            ddlYearLimit.SelectedValue = service.TimeLimited.Split('#')[1];

                        }
                    }
                    else
                    {
                        chkTimelimited.Checked = false;
                        var ddlYearLimit = ServiceDetail11.FindControl("ddlYearLimit") as DropDownList;
                        var ddlMonthLimit = ServiceDetail11.FindControl("ddlMonthLimit") as DropDownList;

                        if (ddlYearLimit != null && ddlMonthLimit != null)
                        {
                            ddlMonthLimit.Enabled = false;
                            ddlYearLimit.Enabled = false;
                        }
                    }
                }


                //ServiceContract1
                var chkContractOutCome = ServiceContract1.FindControl("cklContractOutcome") as CheckBoxList;
                if (chkContractOutCome != null)
                {
                    SetSelectedValueCheckListBox(chkContractOutCome, service.ContractOutcome);
                }

                var cklObligation = ServiceContract1.FindControl("cklContractOutcome") as CheckBoxList;
                if (cklObligation != null)
                    SetSelectedValueCheckListBox(cklObligation, service.ContractObligation);
                var ddlParticipation = ServiceContract1.FindControl("ddlParticipation") as DropDownList;
                if (ddlParticipation != null)
                    ddlParticipation.SelectedValue = service.Participation;

                //ServiceContract1
                var ddlFundingsource = ServiceFunding1.FindControl("ddlFundingsource") as DropDownList;
                if (ddlFundingsource != null)
                {
                    ddlFundingsource.SelectedValue = service.FundingSource;
                }
                var txtFundingNeeds = ServiceFunding1.FindControl("txtFundingNeeds") as TextBox;
                if (txtFundingNeeds != null)
                {
                    if (service.FundingNeeds != 0)
                        txtFundingNeeds.Text = service.FundingNeeds.ToString(CultureInfo.InvariantCulture);

                }
                var lbtLookupContact = ServiceFunding1.FindControl("lbtLookupContact") as LinkButton;
                var lblLookupDetail = ServiceFunding1.FindControl("lblLookupDetail") as Label;
                if (lbtLookupContact != null && lblLookupDetail != null)
                {
                    string announce;
                    var contact = ContactBusiness.Instance.GetContact(service.FundingContactId.ToString(), out announce);
                    if (contact != null)
                    {
                        lblLookupDetail.Text = contact.FirstName + " " + contact.SurName;
                        lbtLookupContact.CommandArgument = contact.ContactId.ToString();
                    }
                }
                var txtAmount = ServiceFunding1.FindControl("txtAmount") as TextBox;
                if (txtAmount != null)
                {

                    txtAmount.Text = service.FundingAmount.ToString(CultureInfo.InvariantCulture);

                }

                var chkContiniuationNeeded = ServiceFunding1.FindControl("chkContiniuationNeeded") as CheckBox;
                if (chkContiniuationNeeded != null)
                {
                    chkContiniuationNeeded.Checked = service.FundingContinuationNeeded;
                }

                var txtFundingContiniuationAmount =
                    ServiceFunding1.FindControl("txtFundingContiniuationAmount") as TextBox;
                if (txtFundingContiniuationAmount != null)
                {
                    txtFundingContiniuationAmount.Text = service.FundingCoutinuationAmount.ToString(CultureInfo.InvariantCulture);
                }

                var txtFundingStart = ServiceFunding1.FindControl("txtFundingStart") as TextBox;
                if (txtFundingStart != null)
                {
                    DateTime fundingStart;
                    txtFundingStart.Text = DateTime.TryParse(service.FundingStart, out fundingStart)
                                               ? fundingStart.ToString("dd/MM/yyyy",CultureInfo.InvariantCulture)
                                               : string.Empty;
                }

                var txtFundingEnd = ServiceFunding1.FindControl("txtFundingEnd") as TextBox;
                if (txtFundingEnd != null)
                {
                    DateTime fundingStart;
                    txtFundingEnd.Text = DateTime.TryParse(service.FundingEnd, out fundingStart)
                                             ? fundingStart.ToString("dd/MM/yyyy",CultureInfo.InvariantCulture)
                                             : string.Empty;
                }

                var txtContiniuationDetails = ServiceFunding1.FindControl("txtContiniuationDetails") as TextBox;
                if (txtContiniuationDetails != null)
                {
                    txtContiniuationDetails.Text = HttpUtility.HtmlDecode(service.FundingContinuationDetails);
                }

                var txtFundraisingForText = ServiceFunding1.FindControl("txtFundraisingForText") as TextBox;
                if (txtFundraisingForText != null)
                {
                    txtFundraisingForText.Text = HttpUtility.HtmlDecode(service.FundraisingForText);
                }
                var txtFundraisingNeeds = ServiceFunding1.FindControl("txtFundraisingNeeds") as TextBox;
                if (txtFundraisingNeeds != null)
                {
                    txtFundraisingNeeds.Text = service.FundraisingNeeds.ToString(CultureInfo.InvariantCulture);
                }
                var txtFundraisingRequiredBy = ServiceFunding1.FindControl("txtFundraisingRequiredBy") as TextBox;
                if (txtFundingEnd != null)
                {
                    DateTime fundingStart;
                    txtFundraisingRequiredBy.Text = DateTime.TryParse(service.FundraisingRequiredBy, out fundingStart)
                                                        ? fundingStart.ToString("dd/MM/yyyy",CultureInfo.InvariantCulture)
                                                        : string.Empty;
                }

                var txtFundraisingWhy = ServiceFunding1.FindControl("txtFundraisingWhy") as TextBox;
                if (txtFundraisingWhy != null)
                {
                    txtFundraisingWhy.Text = HttpUtility.HtmlDecode(service.FundraisingWhy);
                }
                var chkFundraisingComplete = ServiceFunding1.FindControl("chkFundraisingComplete") as CheckBox;
                if (chkFundraisingComplete != null)
                {
                    chkFundraisingComplete.Checked = service.FundraisingComplete;
                }
                var txtFundraisingCompleteDate = ServiceFunding1.FindControl("txtFundraisingCompleteDate") as TextBox;
                if (txtFundraisingCompleteDate != null && txtFundraisingCompleteDate.Enabled)
                {
                    DateTime fundingStart;
                    txtFundraisingCompleteDate.Text = DateTime.TryParse(service.FundraisingCompleteDate,
                                                                        out fundingStart)
                                                          ? fundingStart.ToString("dd/MM/yyyy",CultureInfo.InvariantCulture)
                                                          : string.Empty;
                    txtFundraisingCompleteDate.Enabled = chkFundraisingComplete.Checked;
                }

                var chkDonorAnonymous = ServiceFunding1.FindControl("chkDonorAnonymous") as CheckBox;
                if (chkDonorAnonymous != null)
                {
                    chkDonorAnonymous.Checked = service.FundraisingDonorAnonym;
                }
                var txtFundingDonorDate = ServiceFunding1.FindControl("txtFundingDonorDate") as TextBox;
                if (txtFundingDonorDate != null)
                {
                    DateTime fundingStart;
                    txtFundingDonorDate.Text = DateTime.TryParse(service.FundraisingDonationDate, out fundingStart)
                                                   ? fundingStart.ToString("dd/MM/yyyy",CultureInfo.InvariantCulture)
                                                   : string.Empty;
                }
                var txtFundraisingAmount = ServiceFunding1.FindControl("txtFundraisingAmount") as TextBox;
                if (txtFundraisingAmount != null)
                {
                    txtFundraisingAmount.Text = service.FundraisingDonorAmount.ToString(CultureInfo.InvariantCulture);
                }
                var chkDonationIncremental = ServiceFunding1.FindControl("chkDonationIncremental") as CheckBox;
                if (chkDonationIncremental != null)
                {
                    chkDonationIncremental.Checked = service.FundraisingDonationIncremental;
                }
                //ServiceDetail2
                var cklBenefitsCriterion = ServiceDetail2.FindControl("cklBenefitsCriterion") as CheckBoxList;
                if (cklBenefitsCriterion != null)
                    SetSelectedValueCheckListBox(cklBenefitsCriterion, service.BenefitsCriterion);

                var cklDisability = ServiceDetail2.FindControl("cklDisability") as CheckBoxList;
                if (cklDisability != null)
                    SetSelectedValueCheckListBox(cklDisability, service.Disability);
                var cklBarriersCriterion = ServiceDetail2.FindControl("cklBarriersCriterion") as CheckBoxList;
                if (cklBarriersCriterion != null)
                    SetSelectedValueCheckListBox(cklBarriersCriterion, service.BarriersCriterion);
                var cklPersonalCircumstances = ServiceDetail2.FindControl("cklPersonalCircumstances") as CheckBoxList;
                if (cklBenefitsCriterion != null)
                    SetSelectedValueCheckListBox(cklPersonalCircumstances, service.PersonalCircumstances);
                var cklEthnicityCriterion = ServiceDetail2.FindControl("cklEthnicityCriterion") as CheckBoxList;
                if (cklEthnicityCriterion != null)
                    SetSelectedValueCheckListBox(cklEthnicityCriterion, service.EthnicityCriterion);
                var cklParticipationCriterion = ServiceDetail2.FindControl("cklParticipationCriterion") as CheckBoxList;
                if (cklParticipationCriterion != null)
                    SetSelectedValueCheckListBox(cklParticipationCriterion, service.OtherServiceParticiPationCriterion);
                //ServiceDetail3
                var cklSupportProcess = ServiceDetail3.FindControl("cklSupportProcess") as CheckBoxList;
                if (cklSupportProcess != null)
                    SetSelectedValueCheckListBox(cklSupportProcess, service.ClientSupportProcess);
                var cklOutcome = ServiceDetail3.FindControl("cklOutcome") as CheckBoxList;
                if (cklOutcome != null)
                    SetSelectedValueCheckListBox(cklOutcome, service.ClientOutcome);
                var cklIntervention = ServiceDetail3.FindControl("cklIntervention") as CheckBoxList;
                if (cklIntervention != null)
                    SetSelectedValueCheckListBox(cklIntervention, service.InterventionId);
                var cklTagetClient = ServiceDetail3.FindControl("cklTagetClient") as CheckBoxList;
                if (cklTagetClient != null)
                    SetSelectedValueCheckListBox(cklTagetClient, service.TargetClient);
                var cklClientJourney = ServiceDetail3.FindControl("cklClientJourney") as CheckBoxList;
                if (cklClientJourney != null)
                    SetSelectedValueCheckListBox(cklClientJourney, service.ClientJourney);
                var cklAccreditations = ServiceDetail3.FindControl("cklAccreditations") as CheckBoxList;
                if (cklAccreditations != null)
                    SetSelectedValueCheckListBox(cklAccreditations, service.Accreditations);
                var cklOtherServices = ServiceDetail3.FindControl("cklOtherServices") as CheckBoxList;
                if (cklOtherServices != null)
                    SetSelectedValueCheckListBox(cklOtherServices, service.OtherServiceId);
                var cklReferralSources = ServiceDetail3.FindControl("cklReferralSources") as CheckBoxList;
                if (cklReferralSources != null)
                    SetSelectedValueCheckListBox(cklReferralSources, service.ReferralSources);
                var cklSupportCentres = ServiceDetail3.FindControl("cklSupportCentres") as CheckBoxList;
                if (cklSupportCentres != null)
                    SetSelectedValueCheckListBox(cklSupportCentres, service.SupportCentres);

                var ddlProgramme = ServiceDetail3.FindControl("ddlProgramme") as DropDownList;
                if (ddlParticipation != null)
                    ddlProgramme.SelectedValue = service.ProgrammeId.ToString();
                lbtDetail1.Visible = true;
                lbtDetail2.Visible = true;
                lbtDetail3.Visible = true;
                lbtDetail4.Visible = true;
                lbtDetail5.Visible = true;
                pnDetail1.Visible = true;
                pnDetail2.Visible = false;
                pnDetail3.Visible = false;
                pnDetail4.Visible = false;
                pnDetail5.Visible = false;
                pnContract.Visible = false;
                pnFunding.Visible = false;
                btnInactive.Visible = true;
            }

        }

        /// <summary>
        /// Gets the selected value check list box.
        /// </summary>
        /// <param name="checkBoxList">The check box list.</param>
        /// <returns></returns>
        private string GetSelectedValueCheckListBox(CheckBoxList checkBoxList)
        {
            var result = checkBoxList.Items.Cast<ListItem>().Where(item => item.Selected).Aggregate("",
                                                                                                    (current, item) =>
                                                                                                    current +
                                                                                                    (item.Value + "#"));
            return result.Length - 1 > -1 ? result.Substring(0, result.Length - 1) : result;
        }

        /// <summary>
        /// Sets the selected value check list box.
        /// </summary>
        /// <param name="checkBoxList">The check box list.</param>
        /// <param name="values">The values.</param>
        private void SetSelectedValueCheckListBox(CheckBoxList checkBoxList, string values)
        {
            if (values != null)
            {
                var value = values.Split('#').ToList();
                foreach (ListItem item in checkBoxList.Items)
                {
                    if (value.Contains(item.Value))
                        item.Selected = true;
                }
            }
        }

        /// <summary>
        /// Creates the new service.
        /// </summary>
        /// <returns></returns>
        private Service CreateNewService()
        {
            var service = new Service();

            if (ViewState["serviceId"] != null && ViewState["ModeForm"].ToString() == "Edit")
            {
                service.ServiceId = new Guid(ViewState["serviceId"].ToString());
            }
            else
            {
                service.ServiceId = Guid.NewGuid();
            }
            //ServiceDetail11
            var txtServiceName = ServiceDetail11.FindControl("txtServiceName") as TextBox;
            if (txtServiceName != null)
            {
                if (!string.IsNullOrEmpty(txtServiceName.Text))
                    service.ServiceName =HttpUtility.HtmlEncode(txtServiceName.Text);
                //else
                //{
                //    //thong bao rong
                //}
            }
            var chkActive = ServiceDetail11.FindControl("chkActive") as CheckBox;
            if (chkActive != null)
            {
                service.ServiceActive = chkActive.Checked;
            }

            var txtShortDes = ServiceDetail11.FindControl("txtShortDes") as TextBox;
            if (txtShortDes != null)
            {

                service.ShortDescription = HttpUtility.HtmlEncode(txtShortDes.Text);

            }
            var txtFullDes = ServiceDetail11.FindControl("txtFullDes") as TextBox;
            if (txtFullDes != null)
            {

                service.FullDescription = HttpUtility.HtmlEncode(txtFullDes.Text);

            }
            var ddlSubType = ServiceDetail11.FindControl("ddlSubType") as DropDownList;
            if (ddlSubType != null)
            {
                if (!string.IsNullOrEmpty(ddlSubType.SelectedValue))
                    service.SubType = int.Parse(ddlSubType.SelectedValue,CultureInfo.InvariantCulture);
            }
            var txtDeptCode = ServiceDetail11.FindControl("txtDeptCode") as TextBox;
            if (txtDeptCode != null)
            {

                service.DeptCode = HttpUtility.HtmlEncode(txtDeptCode.Text);

            }

            var lbtLookup = ServiceDetail11.FindControl("lbtLookup") as LinkButton;
            if (lbtLookup != null)
            {
                service.ContactId = !string.IsNullOrEmpty(lbtLookup.CommandArgument)
                                        ? new Guid(lbtLookup.CommandArgument)
                                        : Guid.Empty;
            }
            var ddlServiceType = ServiceDetail11.FindControl("ddlServiceType") as DropDownList;

            if (ddlServiceType != null && !string.IsNullOrEmpty(ddlServiceType.SelectedValue))
                service.ServiceType = int.Parse(ddlServiceType.SelectedValue,CultureInfo.InvariantCulture);

            var txtClientDes = ServiceDetail11.FindControl("txtClientDes") as TextBox;
            if (txtClientDes != null)
            {

                service.ClientDescription = HttpUtility.HtmlEncode(txtClientDes.Text);

            }
            var txtDesDelivery = ServiceDetail11.FindControl("txtDesDelivery") as TextBox;
            if (txtDesDelivery != null)
            {

                service.DescriptionDelivery = HttpUtility.HtmlEncode(txtDesDelivery.Text);

            }
            var ddlAttendace = ServiceDetail11.FindControl("ddlAttendace") as DropDownList;
            if (ddlAttendace != null)
            {

                service.ServiceAttendance = ddlAttendace.SelectedValue;

            }
            var txtContactCode = ServiceDetail11.FindControl("txtContactCode") as TextBox;
            if (txtContactCode != null)
            {

                service.ContractCode = HttpUtility.HtmlEncode(txtContactCode.Text);

            }
            var txtStartExpected = ServiceDetail11.FindControl("txtStartExpected") as TextBox;
            if (txtStartExpected != null)
            {
                DateTime fundingStart;
                if (DateTime.TryParseExact(txtStartExpected.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture,
                                           DateTimeStyles.None, out fundingStart))
                    service.StartExpected = fundingStart.ToString();
                else
                {
                    if (txtStartExpected.Text == string.Empty)
                        service.StartExpected = string.Empty;
                    //thong bao loi dinh dang
                }
            }
            var txtStartDate = ServiceDetail11.FindControl("txtStartDate") as TextBox;
            if (txtStartDate != null)
            {
                DateTime fundingStart;
                if (DateTime.TryParseExact(txtStartDate.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture,
                                           DateTimeStyles.None, out fundingStart))
                    service.StartDate = fundingStart.ToString();
                else
                {
                    if (txtStartDate.Text == string.Empty)
                        service.StartDate = string.Empty;
                    //thong bao loi dinh dang
                }
            }
            var txtEndDate = ServiceDetail11.FindControl("txtEndDate") as TextBox;
            if (txtStartExpected != null)
            {
                DateTime fundingStart;
                if (DateTime.TryParseExact(txtEndDate.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture,
                                           DateTimeStyles.None, out fundingStart))
                    service.EndDate = fundingStart.ToString();
                else
                {
                    if (txtEndDate.Text == string.Empty)
                        service.EndDate = string.Empty;
                    //thong bao loi dinh dang
                }
            }
            var txtContractValue = ServiceDetail11.FindControl("txtContractValue") as TextBox;
            if (txtContractValue != null)
            {

                service.ContractValue = HttpUtility.HtmlEncode(txtContractValue.Text);

            }
            var chkPayment = ServiceDetail11.FindControl("chkPayment") as CheckBox;
            if (chkPayment != null)
            {
                service.StagedPayment = chkPayment.Checked;
            }
            var ddlProcessMethod = ServiceDetail11.FindControl("ddlProcessMethod") as DropDownList;
            if (ddlProcessMethod != null)
            {

                service.ProcessMethod = ddlProcessMethod.SelectedValue;

            }
            var chkExtendable = ServiceDetail11.FindControl("chkExtendable") as CheckBox;
            if (chkExtendable != null)
            {
                if (chkExtendable.Checked)
                {
                    var ddlYearExtendable = ServiceDetail11.FindControl("ddlYearExtendable") as DropDownList;
                    var ddlMonthExtendable = ServiceDetail11.FindControl("ddlMonthExtendable") as DropDownList;

                    if (ddlYearExtendable != null && ddlMonthExtendable != null)
                    {

                        service.Extendable = ddlMonthExtendable.SelectedValue + "#" + ddlYearExtendable.SelectedValue;

                    }
                }
                else
                {
                    service.Extendable = string.Empty;
                }
            }
            var chkTimelimited = ServiceDetail11.FindControl("chkTimelimited") as CheckBox;
            if (chkTimelimited != null)
            {
                if (chkTimelimited.Checked)
                {
                    var ddlYearLimit = ServiceDetail11.FindControl("ddlYearLimit") as DropDownList;
                    var ddlMonthLimit = ServiceDetail11.FindControl("ddlMonthLimit") as DropDownList;

                    if (ddlYearLimit != null && ddlMonthLimit != null)
                    {

                        service.TimeLimited = ddlMonthLimit.SelectedValue + "#" + ddlYearLimit.SelectedValue;

                    }
                }
                else
                {
                    service.TimeLimited = string.Empty;
                }
            }

            //ServiceDetail11
            var chkContractOutCome = ServiceContract1.FindControl("cklContractOutcome") as CheckBoxList;
            service.ContractOutcome = chkContractOutCome != null
                                          ? GetSelectedValueCheckListBox(chkContractOutCome)
                                          : string.Empty;
            var cklObligation = ServiceContract1.FindControl("cklContractOutcome") as CheckBoxList;
            service.ContractObligation = cklObligation != null
                                             ? GetSelectedValueCheckListBox(cklObligation)
                                             : string.Empty;
            var ddlParticipation = ServiceContract1.FindControl("ddlParticipation") as DropDownList;
            service.Participation = ddlParticipation != null ? ddlParticipation.SelectedValue : string.Empty;

            //ServiceFunding1
            var ddlFundingsource = ServiceFunding1.FindControl("ddlFundingsource") as DropDownList;
            service.FundingSource = ddlFundingsource != null ? ddlFundingsource.SelectedValue : string.Empty;

            var txtFundingNeeds = ServiceFunding1.FindControl("txtFundingNeeds") as TextBox;
            if (txtFundingNeeds != null)
            {
                decimal fundingNeeds;
                if (decimal.TryParse(txtFundingNeeds.Text, out fundingNeeds))
                    service.FundingNeeds = fundingNeeds;
            }
            var lbtLookupContact = ServiceFunding1.FindControl("lbtLookupContact") as LinkButton;
            if (lbtLookupContact != null)
            {
                service.FundingContactId = !string.IsNullOrEmpty(lbtLookupContact.CommandArgument)
                                               ? new Guid(lbtLookupContact.CommandArgument)
                                               : Guid.Empty;
            }

            var txtAmount = ServiceFunding1.FindControl("txtAmount") as TextBox;
            if (txtAmount != null)
            {
                decimal amount;
                if (decimal.TryParse(txtAmount.Text, out amount))
                {
                    service.FundingAmount = amount;
                }

            }

            var chkContiniuationNeeded = ServiceFunding1.FindControl("chkContiniuationNeeded") as CheckBox;
            if (chkContiniuationNeeded != null)
            {
                service.FundingContinuationNeeded = chkContiniuationNeeded.Checked;
            }

            var txtFundingContiniuationAmount = ServiceFunding1.FindControl("txtFundingContiniuationAmount") as TextBox;
            if (txtFundingContiniuationAmount != null)
            {
                decimal fundingContiniuationAmount;
                if (decimal.TryParse(txtFundingContiniuationAmount.Text, out fundingContiniuationAmount))
                    service.FundingCoutinuationAmount = fundingContiniuationAmount;
            }

            var txtFundingStart = ServiceFunding1.FindControl("txtFundingStart") as TextBox;
            if (txtFundingStart != null)
            {
                DateTime fundingStart;
                if (DateTime.TryParseExact(txtFundingStart.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture,
                                           DateTimeStyles.None, out fundingStart))
                    service.FundingStart = fundingStart.ToString();
                else
                {
                    if (txtFundingStart.Text == string.Empty)
                        service.FundingStart = string.Empty;
                    //thong bao loi dinh dang
                }
            }

            var txtFundingEnd = ServiceFunding1.FindControl("txtFundingEnd") as TextBox;
            if (txtFundingEnd != null)
            {
                DateTime fundingStart;
                if (DateTime.TryParseExact(txtFundingEnd.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture,
                                           DateTimeStyles.None, out fundingStart))
                    service.FundingEnd = fundingStart.ToString();
                else
                {
                    if (txtFundingEnd.Text == string.Empty)
                        service.FundingEnd = string.Empty;
                    //thong bao loi dinh dang
                }
            }

            var txtContiniuationDetails = ServiceFunding1.FindControl("txtContiniuationDetails") as TextBox;
            if (txtContiniuationDetails != null)
            {
                service.FundingContinuationDetails = HttpUtility.HtmlEncode(txtContiniuationDetails.Text);
            }

            var txtFundraisingForText = ServiceFunding1.FindControl("txtFundraisingForText") as TextBox;
            if (txtFundraisingForText != null)
            {
                service.FundraisingForText = HttpUtility.HtmlEncode(txtFundraisingForText.Text);
            }


            var txtFundraisingNeeds = ServiceFunding1.FindControl("txtFundraisingNeeds") as TextBox;
            if (txtFundraisingNeeds != null)
            {
                decimal fundingContiniuationAmount;
                if (decimal.TryParse(txtFundraisingNeeds.Text, out fundingContiniuationAmount))
                    service.FundraisingNeeds = fundingContiniuationAmount;
                //else
                //{

                //    //loi dinh dang
                //}
            }
            var txtFundraisingRequiredBy = ServiceFunding1.FindControl("txtFundraisingRequiredBy") as TextBox;
            if (txtFundingEnd != null)
            {
                DateTime fundingStart;
                if (DateTime.TryParseExact(txtFundraisingRequiredBy.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture,
                                           DateTimeStyles.None, out fundingStart))
                    service.FundraisingRequiredBy = fundingStart.ToString();
                else
                {
                    if (txtFundraisingRequiredBy.Text == string.Empty)
                        service.FundraisingRequiredBy = string.Empty;
                    //thong bao loi dinh dang
                }
            }

            var txtFundraisingWhy = ServiceFunding1.FindControl("txtFundraisingWhy") as TextBox;
            if (txtFundraisingWhy != null)
            {
                service.FundraisingWhy = HttpUtility.HtmlEncode(txtFundraisingWhy.Text);
            }
            var chkFundraisingComplete = ServiceFunding1.FindControl("chkFundraisingComplete") as CheckBox;
            if (chkFundraisingComplete != null)
            {
                service.FundraisingComplete = chkFundraisingComplete.Checked;
            }
            if (service.FundraisingComplete)
            {
                var txtFundraisingCompleteDate = ServiceFunding1.FindControl("txtFundraisingCompleteDate") as TextBox;
                if (txtFundraisingCompleteDate != null && txtFundraisingCompleteDate.Enabled)
                {
                    DateTime fundingStart;
                    if (DateTime.TryParseExact(txtFundraisingCompleteDate.Text, "dd/MM/yyyy",
                                               CultureInfo.InvariantCulture, DateTimeStyles.None, out fundingStart))
                        service.FundraisingCompleteDate = fundingStart.ToString();
                    else
                    {
                        if (txtFundraisingCompleteDate.Text == string.Empty)
                            service.FundraisingCompleteDate = string.Empty;
                        //thong bao loi dinh dang
                    }
                }
                else
                {
                    service.FundraisingCompleteDate = string.Empty;
                }
            }
            else
            {
                service.FundraisingCompleteDate = string.Empty;
            }
            var chkDonorAnonymous = ServiceFunding1.FindControl("chkDonorAnonymous") as CheckBox;
            if (chkDonorAnonymous != null)
            {
                service.FundraisingDonorAnonym = chkDonorAnonymous.Checked;
            }
            var txtFundingDonorDate = ServiceFunding1.FindControl("txtFundingDonorDate") as TextBox;
            if (txtFundingDonorDate != null)
            {
                DateTime fundingStart;
                if (DateTime.TryParseExact(txtFundingDonorDate.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture,
                                           DateTimeStyles.None, out fundingStart))
                    service.FundraisingDonationDate = fundingStart.ToString();
                else
                {
                    if (txtFundingDonorDate.Text == string.Empty)
                        service.FundraisingDonationDate = string.Empty;
                    //thong bao loi dinh dang
                }
            }
            var txtFundraisingAmount = ServiceFunding1.FindControl("txtFundraisingAmount") as TextBox;
            if (txtFundraisingAmount != null)
            {
                decimal fundingContiniuationAmount;
                if (decimal.TryParse(txtFundraisingAmount.Text, out fundingContiniuationAmount))


                    service.FundraisingDonorAmount = fundingContiniuationAmount;

            }
            var chkDonationIncremental = ServiceFunding1.FindControl("chkDonationIncremental") as CheckBox;
            if (chkDonationIncremental != null)
            {
                service.FundraisingDonationIncremental = chkDonationIncremental.Checked;
            }
            var cklBenefitsCriterion = ServiceDetail2.FindControl("cklBenefitsCriterion") as CheckBoxList;
            service.BenefitsCriterion = cklBenefitsCriterion != null
                                            ? GetSelectedValueCheckListBox(cklBenefitsCriterion)
                                            : string.Empty;
            var cklDisability = ServiceDetail2.FindControl("cklDisability") as CheckBoxList;
            service.Disability = cklDisability != null ? GetSelectedValueCheckListBox(cklDisability) : string.Empty;

            var cklBarriersCriterion = ServiceDetail2.FindControl("cklBarriersCriterion") as CheckBoxList;
            service.BarriersCriterion = cklBarriersCriterion != null
                                            ? GetSelectedValueCheckListBox(cklBarriersCriterion)
                                            : string.Empty;

            var cklPersonalCircumstances = ServiceDetail2.FindControl("cklPersonalCircumstances") as CheckBoxList;
            service.PersonalCircumstances = cklBenefitsCriterion != null
                                                ? GetSelectedValueCheckListBox(cklPersonalCircumstances)
                                                : string.Empty;

            var cklEthnicityCriterion = ServiceDetail2.FindControl("cklEthnicityCriterion") as CheckBoxList;
            service.EthnicityCriterion = cklEthnicityCriterion != null
                                             ? GetSelectedValueCheckListBox(cklEthnicityCriterion)
                                             : string.Empty;

            var cklParticipationCriterion = ServiceDetail2.FindControl("cklParticipationCriterion") as CheckBoxList;
            service.OtherServiceParticiPationCriterion = cklParticipationCriterion != null
                                                             ? GetSelectedValueCheckListBox(cklParticipationCriterion)
                                                             : string.Empty;
            var cklSupportProcess = ServiceDetail3.FindControl("cklSupportProcess") as CheckBoxList;
            service.ClientSupportProcess = cklSupportProcess != null
                                               ? GetSelectedValueCheckListBox(cklSupportProcess)
                                               : string.Empty;
            var cklOutcome = ServiceDetail3.FindControl("cklOutcome") as CheckBoxList;
            service.ClientOutcome = cklOutcome != null ? GetSelectedValueCheckListBox(cklOutcome) : string.Empty;
            var cklIntervention = ServiceDetail3.FindControl("cklIntervention") as CheckBoxList;
            service.InterventionId = cklIntervention != null
                                         ? GetSelectedValueCheckListBox(cklIntervention)
                                         : string.Empty;
            var cklTagetClient = ServiceDetail3.FindControl("cklTagetClient") as CheckBoxList;
            service.TargetClient = cklTagetClient != null ? GetSelectedValueCheckListBox(cklTagetClient) : string.Empty;
            var cklClientJourney = ServiceDetail3.FindControl("cklClientJourney") as CheckBoxList;
            service.ClientJourney = cklClientJourney != null
                                        ? GetSelectedValueCheckListBox(cklClientJourney)
                                        : string.Empty;
            var cklAccreditations = ServiceDetail3.FindControl("cklAccreditations") as CheckBoxList;
            service.Accreditations = cklAccreditations != null
                                         ? GetSelectedValueCheckListBox(cklAccreditations)
                                         : string.Empty;
            var cklOtherServices = ServiceDetail3.FindControl("cklOtherServices") as CheckBoxList;
            service.OtherServiceId = cklOtherServices != null
                                         ? GetSelectedValueCheckListBox(cklOtherServices)
                                         : string.Empty;
            var cklReferralSources = ServiceDetail3.FindControl("cklReferralSources") as CheckBoxList;
            service.ReferralSources = cklReferralSources != null
                                          ? GetSelectedValueCheckListBox(cklReferralSources)
                                          : string.Empty;
            var cklSupportCentres = ServiceDetail3.FindControl("cklSupportCentres") as CheckBoxList;
            service.SupportCentres = cklSupportCentres != null
                                         ? GetSelectedValueCheckListBox(cklSupportCentres)
                                         : string.Empty;
            var ddlProgramme = ServiceDetail3.FindControl("ddlProgramme") as DropDownList;
            if (ddlParticipation != null)
                service.ProgrammeId = !string.IsNullOrEmpty(ddlProgramme.SelectedValue)
                                          ? new Guid(ddlProgramme.SelectedValue)
                                          : Guid.Empty;
            return service;
        }
    }
}